<?php
namespace App\Console\Commands;

use App\Models\BookOrder;
use App\Models\BookRule;
use Illuminate\Console\Command;

class OrderToFinished extends Command{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:finished';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单转完成';



    public function handle()
    {
        $orders = BookOrder::whereHas('rule',function ($query) {
            $query->where('eat_date',now()->format("Y-m-d"));
        })->where('status',BookOrder::ORDER_STATUS_MAKING)->with(['school.rule','rule'])->get();
        foreach ($orders as $order){
            $current = date("H:i:s");
            switch ($order->rule->eat_type){
                case BookRule::EAT_TYPE_BREAKFAST:
                    if($current >= $order->school->rule->morning_end) {
                        $order->finished();
                    }
                    break;
                case BookRule::EAT_TYPE_LUNCH:
                    if($current  >= $order->school->rule->lunch_end) {
                        $order->finished();
                    }
                    break;
                case BookRule::EAT_TYPE_DINNER:
                    if($current >= $order->school->rule->dinner_end) {
                        $order->finished();
                    }
                    break;
            }
        }
    }
}
